/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.jome.formaters.om;

/**
 *
 * @author Mr.Sam
 */
import fr.ove.utils.*;
import fr.ove.openmath.jome.model.*;
import fr.ove.openmath.jome.formaters.om.*;

/**
* Formats the formula tree structure as its corresponding OpenMath object.<BR>
* Format the complex cartesian element.
*
* @author © 2000 DIRAT Laurent
* @version 1.0 25/02/2000
*/
public class ComplexCartesianFormater implements Formater {
    /**
    * Returns the specified object formatted as a string.
    * @param formatedObject the formatted object (for structured object, could represents the beginning).
    * @param formaterRepository where the different other formaters are.
    * @param obj the object to format.
    */
    public String format(String formatedObject, FormaterRepository formaterRepository, Object obj) {
        OpenMathFormater omFormater = (OpenMathFormater) formaterRepository;
        KaryOperator fts = (KaryOperator) obj;
        
        formatedObject += omFormater.writeStartApplication();
        
        // Attention, fts.getOperatorName() retourne le nom de la fonction, mais "(" y est collée.
        // Il faut donc la supprimer.
        // A voir si modif sur le modèle !!!
        String objectId = fts.getTheOperator();
        objectId = objectId.substring(0, objectId.length()-1);
        formatedObject += omFormater.writeSymbol(objectId);
            
        Formater formater = null;
        
        FormulaTreeStructure imaginay = (FormulaTreeStructure) fts.getChild(0); // le slot
        imaginay = (FormulaTreeStructure) imaginay.getChild(0); // l'addition
        FormulaTreeStructure realPart = (FormulaTreeStructure) imaginay.getChild(0);
        imaginay = (FormulaTreeStructure) imaginay.getChild(1); // la multiplication
        imaginay = (FormulaTreeStructure) imaginay.getChild(1); // la partie imaginaire
        
        objectId = realPart.getResourceIdentifier();
        formater = formaterRepository.getFormater(objectId);
        formatedObject = formater.format(formatedObject, formaterRepository, realPart);
        
        objectId = imaginay.getResourceIdentifier();
        formater = formaterRepository.getFormater(objectId);
        formatedObject = formater.format(formatedObject, formaterRepository, imaginay);
        
        formatedObject += omFormater.writeEndApplication();
        
        return formatedObject;
    }
}